package com.pencilisland.pay.mgt.console.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pencilisland.pay.mgt.console.entity.SysRole;
import com.pencilisland.pay.mgt.console.service.RoleService;

/**
 * 角色处理器
 * 
 * @author Breeze
 *
 */
@Controller
@RequestMapping(value = "/console/role")
public class RoleController {

	@Autowired
	RoleService roleService;

	@RequestMapping(value = "")
	public String toRoleList() {
		return "console/role";
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> roleList(@RequestParam(value = "search", required = false) String cretiria,
			@RequestParam(value = "start") int start, @RequestParam(value = "length") int length) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Page<SysRole> rolePage = roleService.roleList(cretiria, start, length);

		resultMap.put("recordsFiltered", rolePage.getTotalElements());
		resultMap.put("recordsTotal", rolePage.getTotalElements());
		resultMap.put("roleList", rolePage.getContent());

		return resultMap;
	}

	@RequestMapping(value = "/obj/list")
	@ResponseBody
	public Map<String, Object> roleObjList(@RequestParam(value = "roleId", required = false) Long roleId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put("roleObjList", roleService.roleObjList(roleId));

		return resultMap;
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public SysRole save(@ModelAttribute SysRole role) {
		return roleService.save(role);
	}

	/**
	 * 加载数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/load/{id}")
	@ResponseBody
	public Map<String, Object> load(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("data", roleService.loadData(id));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	@RequestMapping(value = "/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			roleService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	@RequestMapping(value = "/objs")
	@ResponseBody
	public Map<String, Object> objs(@RequestParam(value = "roleId") Long id,
			@RequestParam(value = "objType") String objType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("objIds", roleService.objs(id, objType));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	@RequestMapping(value = "/objs/save")
	@ResponseBody
	public Map<String, Object> objsSave(@RequestParam(value = "roleId") Long roleId,
			@RequestParam(value = "objIds") String objIds, @RequestParam(value = "objNames") String objNames,
			@RequestParam(value = "objType") String objType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			roleService.objsSave(roleId, objIds,objNames,objType);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/obj/delete/{id}")
	@ResponseBody
	public Map<String, Object> deleteObj(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			roleService.deleteObj(id);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
}
